//
//  HAInvitationVC.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HAInvitationVC.h"

@interface HAInvitationVC ()<UITextFieldDelegate>

/***/
@property (nonatomic, strong) UITextField *t1f;
/***/
@property (nonatomic, strong) UITextField *t2f;
/***/
@property (nonatomic, strong) UITextField *t3f;

/***/
@property (nonatomic, strong) NSString *t1;
/***/
@property (nonatomic, strong) NSString *t2;
/***/
@property (nonatomic, strong) NSString *t3;

/***/
@property (nonatomic, strong) UIView *bgView;


@end

@implementation HAInvitationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.baseTLabel.text = @"新增代理";
    
    [self setupUI];
        
    // Do any additional setup after loading the view.
}

-(void)comBtnClick {
    [self.view endEditing:YES];
    
    if ([NSString isEmptyString:self.t1f.text]) {
        [MBProgressHUD showError:@"请输入服务商名称"];
        return;
    }
    
    if ([NSString isEmptyString:self.t2f.text]) {
        [MBProgressHUD showError:@"请输入服务商手机号"];
        return;
    }
    
    if ([NSString isEmptyString:self.t3f.text]) {
        [MBProgressHUD showError:@"请输入服务商密码"];
        return;
    }
    
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"agent/create";
    request.parms = @{
        @"name":self.t1f.text?:@"",
        @"mobile":self.t2f.text?:@"",
        @"password":self.t3f.text?:@"",
    };
    [MBProgressHUD showLoading:self.view];

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {

            [MBProgressHUD hideHUDForView:self.view];

            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [MBProgressHUD showSuccess:@"提交成功"];
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.t1f) {
        self.t1 = textField.text;
    }
    if (textField == self.t2f) {
        self.t2 = textField.text;
    }
    if (textField == self.t3f) {
        self.t3 = textField.text;
    }
    
}

-(void)setupUI {
    
    NSArray *tits = @[@"服务商名称",@"服务商手机号",@"服务商密码"];
    NSArray *placeholders = @[@"请输入服务商名称",@"请输入服务商手机号",@"请输入服务商密码"];
    CGFloat fristLineTop = app_topH+44+24+54;
    for (int i = 0; i < tits.count; i ++) {
        
        NSString *title = tits[i];
        NSString *placeholder = placeholders[i];
        
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
        [self.view addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.top.mas_equalTo(fristLineTop+54*i);
            make.height.equalTo(@1);
        }];
        
        UILabel *titLabel = [UILabel ha_NLabelWithFont:14 textColor:@"#333333" text:title alignment:0];
        [self.view addSubview:titLabel];
        [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(lineView);
            make.bottom.mas_equalTo(lineView.mas_top).offset(-10);
            make.width.mas_offset(96);
            make.height.mas_offset(32);
        }];
        
        UITextField *textField = [[UITextField alloc]init];
        textField.textColor = [UIColor colorWithHexString:@"#333333"];
        textField.font = [UIFont systemFontOfSize:15];
        textField.placeholder = placeholder;
        textField.delegate = self;
        [self.view addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(titLabel);
            make.left.equalTo(titLabel.mas_right).offset(5);
            make.right.equalTo(lineView);
            make.height.equalTo(@32);
        }];
        
        if ([title isEqualToString:@"服务商名称"]) {
            self.t1f = textField;
        }
        if ([title isEqualToString:@"服务商手机号"]) {
            self.t2f = textField;
        }
        if ([title isEqualToString:@"服务商密码"]) {
            self.t3f = textField;
        }
        
    }
    
    
    UIButton *comBtn = [[UIButton alloc]init];
    [comBtn setTitle:@"提交" forState:UIControlStateNormal];
    [comBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    comBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    comBtn.backgroundColor = [UIColor colorWithHexString:@"#9E312E"];
    comBtn.layer.cornerRadius = 4;
    comBtn.layer.masksToBounds = true;
    [comBtn addTarget:self action:@selector(comBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:comBtn];
    [comBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(42);
        make.right.mas_equalTo(-42);
        make.top.mas_equalTo(fristLineTop+54*3);
        make.height.mas_offset(44);
    }];
    
    UIView *bgView = [UIView ha_createView];
    self.bgView = bgView;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-25);
        make.top.mas_equalTo(comBtn.mas_bottom).offset(20);
        make.height.mas_offset(kUIScreen_h-50);
    }];
    
    UIImageView *imageView = [UIImageView ha_createImageView];
    NSString *codeUrl = [NSString stringWithFormat:@"%@index/auth/register?device=app&agent_id=%@",kHaBaseUrl,HAUserDefaults.agent_id];
    imageView.image = [UIImage generateQRCodeWithString:codeUrl Size:200];
    [bgView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(24);
        make.width.mas_offset(200);
        make.height.mas_offset(200);
    }];
    
    UILabel *label = [UILabel ha_NLabelWithFont:14 textColor:@"333333" text:[NSString stringWithFormat:@"我的邀请码:%@",HAUserDefaults.userID] alignment:1];
    [bgView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(imageView.mas_bottom).offset(20);
    }];
    
    UILabel *label1 = [UILabel ha_NLabelWithFont:12 textColor:@"333333" text:@"长按二维码保存" alignment:1];
    [bgView addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(label.mas_bottom).offset(14);
    }];
    
    UILongPressGestureRecognizer *gesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestures:)];
    gesture.minimumPressDuration=1.0f;
    gesture.numberOfTouchesRequired = 1;
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:gesture];
}

- (void) handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender{
    if (self.bgView) {
        UIImage *shareImage = [self GetmakeImageWithView:self.bgView andWithSize:CGSizeMake(kUIScreen_h-50, kUIScreen_h-50)];
        if (shareImage) {
            UIImageWriteToSavedPhotosAlbum(shareImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }
    }
    
}

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (!error) {
        [MBProgressHUD showSuccess:@"已保存到您的相册"];
    }
}

-(UIImage *)GetmakeImageWithView:(UIView *)view andWithSize:(CGSize)size {
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}



@end
